local desc_fhjd_mode = [[
  # 风华绝代简介
  ___

  规则：以【杀戮尖塔】肉鸽卡牌机制为核心。构建卡组，升级卡牌，遇到随机事件，可双人。

  每人有自己的抽牌堆（游戏开始分配起始卡组到玩家的私人牌堆），弃牌堆（卡牌使用完或者弃置进入此牌堆，该回合玩家仅限洗一次此牌堆牌到抽牌堆），

  消耗牌堆（卡牌或者技能使牌进入此牌堆，本次战斗不再被该角色使用），能量值（玩家卡组都有【费】标记，当【费】值卡牌大于你的能量值即不可使用），卡牌升级标记（玩家卡组每张卡都可以升级，强度表现方式不同）。

  想到什么做什么。

]]
local U = require "packages/utility/utility"

local fhjd_test = false  --开启测试可以自由选将
local whilecards = {"standard","standard_cards","maneuvering","lang_cards"} --白名单卡组
--怪物配置
local MonSters = {{"guojia","xizhicai","simayi","lidian","ol_ex__zhoutai","heqi"}}
--玩家选择
local PlayerGens = {"mini__weiyan"}
--初始卡组
local InitCards = {fearLess=1,bravaDo=1,slash=1,thunder__slash=1,fire__slash=1,defshield=1,strong__slash=1,weak__slash=1,like__slash=1,alone__slash=1,fallThrough=1,equipSelf=1,double__slash=1,shield__slash=1,bd__slash=1,burnHot=1,draw__slash=1,crazy__slash=1,drawdis__slash=1}
--初始能量
local InitCost = 6
--状态牌
local StatusCards = {hurtstatus=function(player) return false end}
--过滤卡牌
local ExcepsCards = {defshield=true,strong__slash=true,weak__slash=true,shield__slash=true,fire_attack=true,jink=true,nullification=true,lightning=true,god_salvation=true,amazing_grace=true,indulgence=true,collateral=true,supply_shortage=true,iron_chain=true,analeptic=true,peach=true}
--升级卡组配置(Cards_Upcfg第一个值是升级该卡标记，第二个是标记值，第三个是费+多少,第四个是初始费)
-- local Status_Marks = {"@up_strongmark","@up_weakmark","@up_defmark","@up_shdmgmark","@up_likeslashmark"}
local Cards_Enum = {"fearLess","bravaDo","fallThrough","equipSelf","burnHot","defshield","strong__slash","weak__slash","like__slash","shield__slash","alone__slash","draw__slash","double__slash","bd__slash","rush__slash","drawdis__slash","crazy__slash"}
local Cards_Upcfg = {slash={"@up_damage",1,0,1},thunder__slash={"@up_damage",1,0,1},fire__slash={"@up_damage",1,0,1},strong__slash={"@up_strongmark",2,0.5,1},
weak__slash={"@up_weakmark",2,0.5,1},defshield={"@up_defmark",1,0,1},shield__slash={"@up_defmark&@up_damage",1,1,1},like__slash={"@up_likeslashmark",1,0,2},alone__slash={"up_damage",2,0,0},
draw__slash = {"@up_damage&@up_drawmark",1,0,1},double__slash = {"@up_atknum",1,0,1},bd__slash = {"@up_damage",1,0,1},rush__slash = {"@sub_cost",1,-1,1},drawdis__slash={"@sub_cost",1,-1,1},
crazy__slash = {"@up_damage",2,0,1},burnHot = {"@up_drawmark",1,0,1},equipSelf={"@@up_equipall",1,0,1},fallThrough={"@sub_cost",1,-1,1},bravaDo={"@up_power",1,0,1},
fearLess={"@up_defmark&@up_drawmark",1,0.5,1}}
--获得可升级卡牌
local getCanUpCards = function(cids)
  local upcards = {}
  for _,cid in ipairs(cids) do
    local card = Fk:getCardById(cid)
    local upstr = Cards_Upcfg[card.name][1]
    local marks = string.split(upstr,"&")
    if not marks then
      if card:getMark(upstr) == 0 then
        table.insert(upcards,cid)
      end
    else
      for _,m in ipairs(marks) do
        if card:getMark(m) == 0 then
          table.insert(upcards,cid)
          break
        end
      end
    end
  end
  return upcards
end
--检测卡牌是否已升级
local isUpCard = function(cid)
  local upcards = getCanUpCards({cid})
  return #upcards == 0 and true or false
end
--改变卡牌升级、降级标记
local changeCardMark = function(upcard,room,add)
  local mark_name = Cards_Upcfg[upcard.name][1]
  local mark_v = Cards_Upcfg[upcard.name][2]
  local cost_v = Cards_Upcfg[upcard.name][3]
  if mark_v > 0 then
    local upstr = Cards_Upcfg[upcard.name][1]
    local marks = string.split(upstr,"&")
    if not marks then
      room:addCardMark(upcard,mark_name,add and mark_v or -mark_v)
    else
      for _,m in ipairs(marks) do
        room:addCardMark(upcard,m,add and mark_v or -mark_v)
      end
    end
  end      
  if cost_v ~= 0 then
    room:addCardMark(upcard,"@play_cost",add and cost_v or -cost_v)
  end
end
--消耗卡牌之前清除标记
local rmMarkBeforeCost = function(cids,room)
  for _, cid in ipairs(cids) do
    local card = Fk:getCardById(cid)
    if next(card.mark) then
      for m,v in pairs(card.mark) do
        if v ~= 0 then
          room:setCardMark(card,m,0)
        end
      end
    end
  end
end
--消耗卡牌到void区
local handleMoveCards = function(cids,fromArea,toArea,from,to,skiname)
  local room = from and from.room or to.room
  room:moveCards({
    from = from and from.id or nil,
    to = to and to.id or nil,
    ids = cids,
    fromArea = Card.PlayerHand,
    toArea = Card.Void,
    moveReason = fk.ReasonJustMove,
    proposer = from.id,
    skillName = skiname,
  })
end
--获得复制的临时卡牌（进入弃牌堆过程会被丢尽void区域）
local handleGetTmpCards = function(cids,from,putarea,skiname)
  for _,cid in ipairs(cids) do
    local card = Fk:getCardById(cid)
    room:setCardMark(card,"card_owner",from.id)
    room:setCardMark(card,"card_tmp",1)
    from:addToPile(putarea, {cid}, true, skiname)
  end
end

local fhjd_getLogic = function()
  local fhjd_logic = GameLogic:subclass("fhjd_logic")

  function fhjd_logic:initialize(room)
    GameLogic.initialize(self, room)
    self.role_table[6] = { "lord", "rebel", "rebel", "rebel", "rebel", "rebel"}
  end

  function GameLogic:prepareForStart()
    local room = self.room
    local players = room.players

    self:addTriggerSkill(GameRule)
    for _, trig in ipairs(Fk.global_trigger) do
      if trig.name ~= "mobile_effect" then  --关闭手杀特效
        self:addTriggerSkill(trig)
      end
    end
    self.room:sendLog{ type = "$GameStart" }
  end

  function fhjd_logic:prepareDrawPile()
    local room = self.room
    local allCardIds = Fk:getAllCardIds()
    --重新过滤卡组
    local filterCardIds = {}
    local while_cards = table.clone(whilecards)
    for i = #allCardIds, 1, -1 do
      local id = allCardIds[i]
      local card = Fk:getCardById(id)
      if card then
        if card.is_derived or not table.contains(while_cards,card.package.name) or ExcepsCards[card.name] or table.contains(Cards_Enum,card.name) then
          -- print(card.name)
          table.insert(room.void, id)
          room:setCardArea(id, Card.Void, nil)
        else
          table.insert(filterCardIds,id)
        end
      end
      -- if table.contains(while_cards,card.package.name) and not ExcepsCards[card.name] then
      --   if not card.is_derived then
      --     table.insert(filterCardIds,id)
      --     -- print("加入卡组")
      --     -- print(card.name)
      --   end
      -- end
    end
    table.shuffle(filterCardIds)
    room.draw_pile = filterCardIds
    self.total_cards = allCardIds
    for _, id in ipairs(room.draw_pile) do
      room:setCardArea(id, Card.DrawPile, nil)
    end
  end

  function fhjd_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles = self.role_table[n]
    roles = table.clone(roles)
    table.shuffle(roles)
    local humans = {}
    local robots = {}
    for _,pr in ipairs(room.players) do
      if pr.id > 0 then 
        table.insert(humans,pr)
      else
        table.insert(robots,pr)
      end
    end
    -- if #humans > 2 then  return end --默认主反模式
    --风华绝代
    local randLord = table.random(humans)
    randLord.role = "lord"
    randLord.role_shown = true
    room:broadcastProperty(randLord, "role")
    table.removeOne(humans,randLord)
    if #humans > 0 then
      for _,man in pairs(humans) do
        man.role = "loyalist"
        man.role_shown = true
        room:notifyProperty(man, man, "role")
        room:broadcastProperty(man, "role")
      end
    end
    for _,robot in ipairs(robots) do
      robot.role = "rebel"
      robot.role_shown = true
      room:notifyProperty(robot, robot, "role")
      room:broadcastProperty(robot, "role")
    end
  end

  function fhjd_logic:chooseGenerals()
    local room = self.room
    -- local generalNum = room.settings.preferredLuckTime
    local generalNum = 6
    local lord = room:getLord()
    room.current = lord
    local nolords = {}
    local tmpdeads = {"tycl__caocao","tianchou","caimaozhangyun","malingli","caoyi","zhouyi","mini__lvmeng","mini__weiyan","l__sunshangxiang"}
    local msters = table.clone(MonSters)
    local pgens = table.random(PlayerGens)
    table.shuffle(msters[1])
    for _, p in ipairs(room.players) do
      if p.role ~= "lord" then
        table.insert(nolords,p)
      end
    end
    self.monsters = msters

    local handleSelGen = function(who,g)
      room:setPlayerGeneral(who, g, true)
      room:askForChooseKingdom({who})
      room:broadcastProperty(who, "general")
      room:broadcastProperty(who, "kingdom")
    end
    handleSelGen(lord,pgens)
    for _,pr in ipairs(nolords) do
      if pr.id < 0 then
        handleSelGen(pr,table.remove(tmpdeads,1))
      else
        -- local filtgens = table.random(whilegens,generalNum) 
        -- local selgen = room:askForGeneral(pr, filtgens, 1,true)
        -- table.removeOne(whilegens,selgen)
        -- handleSelGen(pr,selgen)
      end
    end
  end
  return fhjd_logic
end
--引擎
local CurPhaseEnd = function (target,card)
  if target.phase == Player.NotActive then
      return
  end
  local room = target.room
  local mark = target:getMark("@fhjd_use_num")
  local cardmark = card:getMark("@play_cost")
  if cardmark and type(cardmark) == "number" then
    mark = mark - cardmark
    room:setPlayerMark(target,"@fhjd_use_num",mark)
  end
end
--找牌组，找不到就印卡优化牌
local findCards = function(player,needpiles,piles)
  local room = player.room
  local obtain_cards = {}
  local value = 0
  for k,v in pairs(needpiles) do
    value = v + value
  end
  for _,cid in ipairs(piles) do
    local card = Fk:getCardById(cid)
    local remain_num = needpiles[card.name]
    if remain_num and remain_num > 0 then
      table.insert(obtain_cards,cid)
      value = value -1 
      needpiles[card.name] = needpiles[card.name] - 1
    end
    if value <= 0 then break end
  end
  if value > 0 then
    for cname,num in pairs(needpiles) do
      if num > 0 then
        for i = 1,num do
          local cid = room:printCard(cname, math.random(1,4), math.random(1,13)).id
          table.insert(obtain_cards,cid)
        end
      end
    end
  end
  return obtain_cards
end
--获取到的卡牌放进私人牌堆
local inputPipleByGet = function(player,cards,pilename,skiname)
  local room = player.room
  if not cards or #cards == 0 then room:doBroadcastNotify("ShowToast","警告，找不到卡组") return end
  for _,cid in ipairs(cards) do
    local card = Fk:getCardById(cid)
    if Cards_Upcfg[card.name] then
      room:setCardMark(card,"@play_cost",Cards_Upcfg[card.name][4])
    end
    room:setCardMark(card,"card_owner",player.id)
  end
  player:addToPile(pilename, cards, true, skiname)
end
--从私人牌堆获取任意卡牌
local drawCardsFromPile = function(player,num,pile)
  local drawpiles = pile or player:getPile("self_draws")
  return table.random(drawpiles,num)
end

local fhjd_yinqing = fk.CreateTriggerSkill{
  name = "fhjd_yinqing",
  frequency = Skill.Compulsory,
  events = {fk.BeforeCardsMove,fk.AfterCardsMove,fk.TurnStart,fk.TurnEnd,fk.CardUseFinished,fk.AfterDrawNCards,fk.GameStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then
      return
    end
    if event == fk.BeforeCardsMove then
      for _, move in ipairs(data) do
        if fk.ReasonDraw == move.moveReason and move.to and move.to == player.id and move.toArea and move.toArea == Card.PlayerHand then
          if not move.skillName then
              return true
          end
          if move.skillName and move.skillName ~= self.name then
              return true
          end
        end
      end
    end
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          local todiscpiple = {{},{}}
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            local ownerid = card:getMark("card_owner")
            local cardtmp = card:getMark("card_tmp")
            if ownerid > 0 and player.id == ownerid then
              if cardtmp == 0 then
                table.insert(todiscpiple[1],info.cardId)
              else
                table.insert(todiscpiple[2],info.cardId)
              end
            end
          end
          if #todiscpiple[1] > 0 or #todiscpiple[2] > 0 then
            self.cost_data = todiscpiple
            return true
          end
        end
      end
    end
    if event == fk.TurnStart or event == fk.TurnEnd then
      return player == target and player.room.current == target
    elseif event == fk.GameStart or event == fk.AfterDrawNCards then
      return player == target 
    elseif event == fk.CardUseFinished then
      return player == target 
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.BeforeCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if fk.ReasonDraw == move.moveReason and move.to and move.to == player.id and move.toArea and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.DrawPile then
                table.insert(ids, info.cardId)
            end
          end
          if #ids > 0 then
            move.moveInfo = {}
            local draw_pile_num = math.min(#ids,#player:getPile("self_draws"))
            if draw_pile_num == 0 then room:doBroadcastNotify("ShowToast","没牌可以抽了") return end
            local drawpile_cards = drawCardsFromPile(player,draw_pile_num)
            -- handleMoveCards(drawpile_cards,Card.PlayerSpecial,Card.PlayerHand,player,player,self.name)
            room:moveCards({
              from = player.id,
              ids = drawpile_cards,
              fromArea = Card.PlayerSpecial,
              to = player.id,
              toArea = Card.PlayerHand,
              moveReason = fk.ReasonJustMove,
              proposer = player.id,
              skillName = self.name,
            })
          end
        end
      end
    end
    if event == fk.AfterCardsMove then
      if self.cost_data and #self.cost_data[1] > 0 then
        player:addToPile("self_discs", self.cost_data, true, self.name)
      end
      if self.cost_data and #self.cost_data[2] > 0 then
        p(self.cost_data[2])
        dbg()
        for _, cid in ipairs(self.cost_data[2]) do
          room:setCardMark(Fk:getCardById(cid),"card_tmp",0)
        end
        room:moveCards({
          from = player.id,
          ids = self.cost_data[2],
          -- fromArea = Card.PlayerSpecial,
          -- to = player.id,
          toArea = Card.Void,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = self.name,
        })
      end
    end
    if event == fk.TurnStart then
      room:setPlayerMark(target,"@fhjd_use_num",InitCost)
    elseif event == fk.GameStart then

    elseif event == fk.AfterDrawNCards then
      local draw_num = player:getMark("fhjd_draw")
      local draw_pile = player:getPile("self_draws")
      if draw_num > #draw_pile then --摸牌阶段抽牌区不够抽
        local discs_pile = player:getPile("self_discs")
        if #draw_pile > 0 then
          room:moveCards({
            from = player.id,
            ids = draw_pile,
            fromArea = Card.PlayerSpecial,
            to = player.id,
            toArea = Card.PlayerHand,
            moveReason = fk.ReasonJustMove,
            proposer = player.id,
            skillName = self.name,
          })
        end
        if #discs_pile == 0 then return end
        room:moveCards({
          from = player.id,
          ids = discs_pile,
          fromArea = Card.PlayerSpecial,
          -- to = player.id,
          toArea = Card.Processing,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = self.name,
        })
        room:delay(200)
        local new_draws = {}
        local i = 1
        while i <= draw_num-#draw_pile and #discs_pile > 0 do
          local r = math.random(1, #discs_pile)
          table.insert(new_draws, table.remove(discs_pile, r))
          i = i+1
        end
        room:moveCards({
          from = player.id,
          ids = new_draws,
          fromArea = Card.Processing,
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = self.name,
        })
        player:addToPile("self_draws", discs_pile, true, self.name)
      else  --摸牌阶段抽牌
        local drawcards = table.random(draw_pile,draw_num)
        room:moveCards({
          from = player.id,
          ids = drawcards,
          fromArea = Card.PlayerSpecial,
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = self.name,
        })
      end
    elseif event == fk.CardUseFinished then
      CurPhaseEnd(target,data.card)
    elseif event == fk.TurnEnd then
      room:setPlayerMark(target,"@fhjd_use_num",0)
    end
  end,
}
local fhjd_yinqing_prohibit = fk.CreateProhibitSkill{
  name = "#fhjd_yinqing_prohibit",
  -- frequency = Skill.Compulsory,
  prohibit_use = function(self, player, card)
    if player:hasSkill(self) then
      local cardowner = card:getMark("card_owner")
      if cardowner == player.id then
        return card:getMark("@play_cost") > player:getMark("@fhjd_use_num")
      else
        return true
      end
    end
  end,
}

fhjd_yinqing:addRelatedSkill(fhjd_yinqing_prohibit)
Fk:addSkill(fhjd_yinqing)
local fhjd_nolimit = fk.CreateTargetModSkill{
  name = "fhjd_nolimit",
  -- frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope)
    return true
  end,
}
Fk:addSkill(fhjd_nolimit)
--重置状态
local resetBury = function(who,exceptlist)
  who:setSkillUseHistory("")
  who:setCardUseHistory("")
  who:throwAllCards()
  if exceptlist then
    for mark,v in pairs(who.mark) do
      if not table.contains(exceptlist,mark) then
        self.room:setPlayerMark(who, mark, 0)
      end
    end
  else
    who:throwAllMarks()
  end
  who:clearPiles()
  who:reset()
end
--去除相关技能
local removeRelateSkills = function(who)
  local room = who.room
  local og = Fk.generals[who.general]
  local to_rm = table.map(og.related_skills, Util.NameMapper)
  table.insertTable(to_rm, og.related_other_skills)
  if #to_rm > 0 then
    room:handleAddLoseSkills(who, table.concat(table.map(to_rm, function(s) return "-" .. s end), "|"), nil, true)
  end
end
--转变敌人
local changeEmy = function(who,montype)
  local room = who.room
  local monsters = room.logic.monsters
  if #monsters == 0 then return end
  local newmonster = table.remove(monsters[montype],1)
  room:changeHero(who, newmonster, true, false, true)
  room:revivePlayer(who, false)
  room:setPlayerProperty(who, "kingdom", Fk.generals[newmonster].kingdom)
  room:askForChooseKingdom({who})
  return newmonster
end
--删掉旧技能
local removeOldSkills =function(newgeneral,who)
  local room = who.room
  local gen = Fk.generals[newgeneral]
  local otherskills = gen.other_skills
  local rm_skills = {}
  local new_gen_skills = table.map(gen.skills,function (ski)
    return ski.name
  end)
  if otherskills and #otherskills > 0 then
    table.insertTableIfNeed(new_gen_skills,otherskills)
  end
  for _, skill in ipairs(who.player_skills) do
    if not table.contains(new_gen_skills,skill.name) then
      table.insert(rm_skills,"-"..skill.name.."|")
    end
  end
  if #rm_skills > 0 then
    local loseskills = table.concat(rm_skills,"")
    room:handleAddLoseSkills(who, loseskills, nil, false)
  end
end
--刷新最大体力
local updateMax = function(who,value,mes)
  local room = who.room
  who.hp = who.hp + value
  who.maxHp = who.maxHp + value
  room:setPlayerProperty(who, "hp", who.hp)
  room:setPlayerProperty(who, "maxHp", who.maxHp)
end
--复活的敌人
local relifeEmy = function(player,montype)
  -- local maxhp = player.maxHp
  resetBury(player)
  removeRelateSkills(player)
  local newEmy = changeEmy(player,montype)
  if not newEmy then room:gameOver("lord") return end
  removeOldSkills(newEmy,player)
  updateMax(player,1)
  return true
end
--遭遇普通敌人
local meetNorEmy = function(player,emynum)
  local room = player.room
  if not relifeEmy(player,1) then room:doBroadcastNotify("ShowToast","敌人复活失败") return end
  if emynum - 1 <= 0 then return end
  local deademys = table.filter(room.players,function(pr)
    return pr.id < 0 and pr.dead
  end)
  if #deademys == 0 then room:doBroadcastNotify("ShowToast","没有敌人可复活了") return end
  local randrelife_emys = table.random(deademys,emynum - 1)
  for _,re_emy in ipairs(randrelife_emys) do
    if not relifeEmy(re_emy,1) then room:doBroadcastNotify("ShowToast","敌人复活失败") return end
  end
end
--玩家印卡获得新卡牌
local getNewCardByPrint = function(player,num,selchc)
  local room = player.room
  for i = 1,num do
    local selcard_chs = room:askForChoice(player,selchc,"#fhjd_rule","#fhjd_selcards",true)
    -- local selcards = findCards({[selcard_chs] = 1},room.void)
    local selcards = findCards(player,{[selcard_chs] = 1},room.void)
    inputPipleByGet(player,selcards,"self_discs","#fhjd_rule")
    -- inputPipleByGet(player,{room:printCard(selcard_chs, math.random(1,4), math.random(1,13)).id},"self_discs","#fhjd_rule")
  end
end
--去除卡牌临时升级标记
local reMoveUpByTmp = function(player)
  local room = player.room
  local tmpupcards = room.logic.tmpupcards
  local upcards = tmpupcards[tostring(player.id)]
  if tmpupcards and #upcards > 0 then
    for _,cid in ipairs(upcards) do
      local upcard = Fk:getCardById(cid)
      changeCardMark(upcard,room,false)
    end
    upcards[tostring(player.id)] = {}
  end
end
--玩家升级卡牌
local upCardByPile = function(player,num,pile)
  local room = player.room
  local can_upcards = player:getPile(pile)
  for i=1,num do
    local upcards = getCanUpCards(can_upcards)
    if #upcards == 0 then room:doBroadcastNotify("ShowToast","没有卡牌可升级") return end
    -- upcards = U.askForArrangeCards(player, "fhjd_upcard", upcards, "#fhjd_upcard-choose", false, 0, {#upcards,1}, {0, 1}, ".", "shelie", {{}, get})[2]
    upcards = room:askForCardsChosen(player, player, 1, 1, {card_data = {{"fhjd_yinqing", upcards}}}, "fhjd_yinqing", "#fhjd_upcard-choose")
    if upcards and #upcards > 0 then
      local upcard = Fk:getCardById(upcards[1])
      changeCardMark(upcard,room,true)
    end
  end
end
--风华绝代模式
local fhjd_rule = fk.CreateTriggerSkill{
  name = "#fhjd_rule",
  priority = 0.001,
  refresh_events = {fk.GameStart,fk.DrawNCards,fk.EventPhaseStart,fk.TurnStart,fk.TargetSpecified,fk.DrawInitialCards,fk.Deathed,fk.GameOverJudge},
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player == target and player.role == "lord"
    elseif event == fk.EventPhaseStart and target == player and player.phase == Player.Discard then
      return true
    elseif event == fk.DrawNCards or event == fk.DrawInitialCards or event == fk.TargetSpecified then
      return player == target
    elseif event == fk.TurnStart then
      return player == target and player.room.current == target
    elseif event == fk.GameOverJudge then
      return player.role == "lord"
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local all_selgens = room.logic.all_selgens
      local humans = table.filter(room.players,function(pr)
        if pr.id > 0 then return true end
      end)
      local human_gens = table.map(humans,function(pr)
        return pr.general
      end)
      -- if not GensLimts:gensIsInConf(human_gens,all_selgens) or not all_selgens or #humans > 2 then 
      --   room:doBroadcastNotify("ShowToast", "选将不在原选将框内或不符合游戏规则，游戏结束")
      --   room:gameOver("lord")
      --   return
      -- end
      room:setTag("SkipGameRule", true)
      local rand_active_ai = table.random(table.filter(room.players,function(pr)
        if pr.id < 0 then return true end
      end))
      room.logic.tmpupcards = {}
      for i,gen in ipairs(room.players) do
        if gen.id > 0 then
          room.logic.tmpupcards[tostring(gen.id)] = {}
          gen.hp = gen.hp + 8
          gen.maxHp = gen.maxHp + 8
          room:setPlayerProperty(gen, "hp", gen.hp)
          room:setPlayerProperty(gen, "maxHp", gen.maxHp)
          room:handleAddLoseSkills(gen, "fhjd_yinqing|fhjd_nolimit", nil, false)
          room:setPlayerMark(gen,"fhjd_draw",5)
          local cards = findCards(gen,table.clone(InitCards),room.logic.total_cards)
          -- local cards = findCards(gen,table.clone(InitCards),room.draw_pile)
          inputPipleByGet(gen,cards,"self_draws",self.name)  --摸5牌
          --从牌堆获得卡牌
          -- getNewCardByPrint(gen,1,table.random(Cards_Enum,3))
          --升级卡牌
          upCardByPile(gen,2,"self_draws")
        else
          if gen ~= rand_active_ai then
            room:killPlayer({ who = gen.id })
          else
            room:handleAddLoseSkills(gen, "fhjd_nolimit", nil, false)
          end
        end
      end
    elseif event == fk.GameOverJudge then
      room:setTag("SkipGameRule", true)
      local room = player.room
      if not table.every(room:getOtherPlayers(target),function(pr) return pr.id > 0 end) then return end
      if player.dying then room:gameOver("rebel") return end
      --去除卡牌临时升级标记
      reMoveUpByTmp(player)
      --本关过丢所有手牌
      player:throwAllCards("h")
      --选择卡牌
      getNewCardByPrint(player,1,table.random(Cards_Enum,3))
      --选择下一关路线
      local roads = {"normonster","strongmonster","boss","box","events","shop","rest"}
      local selroad = room:askForChoice(player,roads,self.name,"#mapselect",true)
      meetNorEmy(target,2)
      -- if selroad == "normonster" then
        
      -- else
      -- end
    elseif event == fk.EventPhaseStart and target == player and player.phase == Player.Discard then
      --状态牌（诅咒）
      for _,cid in ipairs(player.player_cards[Player.Hand]) do
        local card = Fk:getCardById(cid)
        if StatusCards[card.name] then
          StatusCards[card.name](player)
        end
      end
      player:throwAllCards("h")
    elseif event == fk.DrawInitialCards then
      data.num = 0
    elseif event == fk.TurnStart then
      if room.current.id > 0 then
        room.current.shield = 0
        room:setPlayerProperty(room.current, "shield", room.current.shield)
        for _,pr in ipairs(room.alive_players) do
          if pr.id < 0 then --敌人每回合获取牌
            pr:addToPile("emy_atk_cards", findCards(player,{slash=3},room.draw_pile), true, self.name)
            -- pr:addToPile("emy_atk_cards", drawCardsFromPile(pr,6,room.draw_pile), true, self.name) 
          end
        end
      else
        room:moveCards({
          from = room.current.id,
          ids = room.current:getPile("emy_atk_cards"),
          fromArea = Card.PlayerSpecial,
          to = room.current.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = room.current.id,
          skillName = self.name,
        })
      end
    elseif event == fk.DrawNCards then
      data.n = 0
    elseif event == fk.TargetSpecified then
      data.disresponsive = true
    end
  end,
}

Fk:addSkill(fhjd_rule)
local FHJD_MODE = fk.CreateGameMode{
  name = "FHJD_MODE",
  minPlayer = 6,
  maxPlayer = 6,
  rule = fhjd_rule,
  logic = fhjd_getLogic,
  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 10s", passed = playedTime >= 10 } }
    if Self.role ~= "lord" then
      table.insert(surrenderJudge, { text = "only you alive", passed = #Fk:currentRoom().alive_players <= 2 })
    end
    return surrenderJudge
  end,
}

--------------------------------风华绝代卡组-----------------------------------------
--------------------------------风华绝代卡组-----------------------------------------
--------------------------------风华绝代卡组-----------------------------------------
--------------------------------风华绝代卡组-----------------------------------------
local Fhjd_Mode = "fhjd_logic"
local fhjdExtraAdd = function(rm,cd,mk)
  if rm.logic.class.name == Fhjd_Mode and cd then
    return cd.mark[mk] or 0
  end
  return 0
end
local clone_slash = Fk:cloneCard("slash")
local clone_ex_nihilo = Fk:cloneCard("ex_nihilo")
-----------------修改杀的效果函数-------------
local slash_eff_common = function(self, room, effect,damagetype,value)
  local from = room:getPlayerById(effect.from)
  local to = room:getPlayerById(effect.to)
  local extra_add = fhjdExtraAdd(room,effect.card,"@up_damage")
  if not to.dead then
    room:damage({
      from = from,
      to = to,
      card = effect.card,
      damage = value + extra_add,
      damageType = damagetype,
      skillName = self.name
    })
  end
end
Fk.skills["slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,1)
end
Fk.skills["thunder__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.ThunderDamage,1)
end
Fk.skills["fire__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.FireDamage,1)
end

-----------------修改杀的效果函数-------------
-----------------目标为自己卡牌效果函数-------------
--护甲
Fk.skills["defshield_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  local extra_add = fhjdExtraAdd(room,effect.card,"@up_defmark")
  target.shield = target.shield + 1 + extra_add
  room:broadcastProperty(target, "shield")
end
--拔刀杀
Fk.skills["bdslash_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  local tars = room:getOtherPlayers(target)
  for _,t in ipairs(tars) do
    if not GensLimts:isSameCamp(target,t) then
      local extra_add = fhjdExtraAdd(room,effect.card,"@up_damage")
      room:damage({
      from = target,
      to = t,
      card = effect.card,
      damage = 1+extra_add,
      damageType = fk.NormalDamage,
      skillName = self.name
    })
    end
  end
end
--燃烧
Fk.skills["burn_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  -- room:loseHp(target,losehp,self.name)
  local distovoid = room:askForCard(target, 1, 1, false, self.name, false, ".", "#burn_skill")
  if isUpCard(distovoid[1]) then
    rmMarkBeforeCost(distovoid,room)
  end
  handleMoveCards(distovoid,Card.PlayerHand,Card.Void,target,nil,self.name)
  room:drawCards(target,2+fhjdExtraAdd(room,effect.card,"@up_drawmark"),self.name)
end
--武装
Fk.skills["equipself_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
    if target.dead or not room.logic.tmpupcards then room:doBroadcastNotify("ShowToast","不是风华绝代模式无效果") return end
    local tmpupcards = room.logic.tmpupcards[tostring(target.id)]
    if not tmpupcards then room:doBroadcastNotify("ShowToast","不是风华绝代模式无效果") return end
    local upcards = getCanUpCards(target.player_cards[Player.Hand])
    if #upcards == 0 then room:doBroadcastNotify("ShowToast","没有卡牌可升级") return end
    if fhjdExtraAdd(room,effect.card,"@@up_equipall") == 1 then
      table.insertTable(tmpupcards,upcards)
      --临时升级（本次战斗）
      for _,cid in ipairs(upcards) do
        local upcard = Fk:getCardById(cid)
        changeCardMark(upcard,room,true)
      end
    else
      local upcards = U.askForArrangeCards(target, "fhjd_upcard", upcards, "#fhjd_upcard-choose", false, 0, {#upcards,1}, {0, 1}, ".", "shelie", {{}, get})[2]
      local upcard = Fk:getCardById(upcards[1])
      changeCardMark(upcard,room,true)
    end
end
Fk.skills["bravado_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  room:addPlayerMark(target,"@hfjd_power",1+fhjdExtraAdd(room,effect.card,"@up_power"))
  room:handleAddLoseSkills(target, "bravadoskill", nil, false)
end
--破灭
Fk.skills["fallthrough_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  local draw_piples = target:getPile("self_draws")
  if #draw_piples > 0 then
    local drawid = table.random(draw_piples)
    handleMoveCards({drawid},Card.PlayerSpecial,Card.Processing,target,nil,self.name)
    local usecard = Fk:getCardById(drawid)
    local effect = {from=effect.from,card=usecard}
    local onEffect = usecard.skill.onEffect
    if usecard.trueName == "slash" then
      local other = room:getOtherPlayers(target)
      local choose
      if #other == 1 then
        choose = {other[1].id}
      else
        choose = room:askForChoosePlayers(target,table.map(other,function(p) return p.id end),1,1,"#fallthrough_skill_choose",self.name,false)
      end
      effect.to = choose[1]
      onEffect(usecard.skill,room,effect)
    else
      effect.to = effect.to
      onEffect(usecard.skill,room,effect)
    end
    handleMoveCards({drawid},Card.Processing,Card.Void,target,nil,self.name)
  end
end
--无畏
Fk.skills["fearless_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  local extra_add = fhjdExtraAdd(room,effect.card,"@up_defmark")
  target.shield = target.shield + 1 + extra_add
  room:broadcastProperty(target, "shield")
  room:drawCards(target,1+fhjdExtraAdd(room,effect.card,"@up_drawmark"),self.name)
end
--坚毅
Fk.skills["unwaver_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  local extra_add = fhjdExtraAdd(room,effect.card,"@up_defmark")
  target.shield = target.shield + 1 + extra_add
  room:broadcastProperty(target, "shield")
  local distovoid = room:askForCard(target, 1, 1, false, self.name, false, ".", "#cost_cards")
  if isUpCard(distovoid[1]) then
    rmMarkBeforeCost(distovoid,room)
  end
  handleMoveCards(distovoid,Card.PlayerHand,Card.Void,target,nil,self.name)
end
--双持
Fk.skills["clonedouble_skill"].onEffect = function(self, room, effect)
  local target = room:getPlayerById(effect.to)
  if target.dead then return end
  if target:hasSkill("fhjd_yinqing") then
    if target.player_cards[Player.Hand] > 0 then
      local selcards = room:askForCard(target, 1, 1, false, self.name, false, ".", "#clone_cards")
      -- handleGetTmpCards(selcards,target,"self_draws",self.name)
      local cards = findCards(target,{[Fk:getCardById(selcards[1]).name] = 1},room.void)
      local cards_area = room:getCardArea(cards[1]) 
      handleMoveCards(cards,cards_area,Card.PlayerHand,nil,target,self.name)
    end
    
  end
end
-----------------目标为自己卡牌效果函数-------------
------------------各种伤害杀效果函数----------------
--猛杀
Fk.skills["strong__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,1)
  local tar = room:getPlayerById(effect.to)
  room:addPlayerMark(tar,"@strong_hurt",2 + (effect.card.mark["@up_strongmark"] or 0))
  room:handleAddLoseSkills(tar, "strong_hurt_skill", nil, false)
end
--残杀
Fk.skills["weak__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,1)
  local tar = room:getPlayerById(effect.to)
  room:addPlayerMark(tar,"@weak_hurt",2+(effect.card.mark["@up_weakmark"] or 0))
  room:handleAddLoseSkills(tar, "weak_hurt_skill", nil, false)
end
--盾杀
Fk.skills["shield__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,1)
  local recfrom = room:getPlayerById(effect.from)
  if not recfrom.dead then
    recfrom.shield = recfrom.shield + 1 + fhjdExtraAdd(room,effect.card,"@up_defmark")
    recfrom.room:broadcastProperty(recfrom, "shield")
  end
end
--杀意
Fk.skills["like__slash_skill"].onEffect = function(self, room, effect)
  local to = room:getPlayerById(effect.to)
  local from = room:getPlayerById(effect.from)
  local extra_add,slahnum = 0,0
  local fromhands = from.player_cards[Player.Hand]
  if #fromhands > 0 then
    for _,cid in ipairs(fromhands) do
      local card = Fk:getCardById(cid)
      if card.trueName == "slash" then
        slahnum = slahnum + 1
      end
    end
  end
  local upmark = effect.card.mark["@up_likeslashmark"]
  if upmark then
    extra_add = math.floor(slahnum / 2)
  else
    extra_add = math.floor(slahnum / 3)
  end  
  room:damage({
    from = from,
    to = to,
    card = effect.card,
    damage = 1+extra_add,
    damageType = fk.NormalDamage,
    skillName = self.name
  })
end
--独杀
Fk.skills["alone__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,3)
end
--抽杀
Fk.skills["draw__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,1)
  local from = room:getPlayerById(effect.from)
  if not from.dead then
    room:drawCards(from, 1+fhjdExtraAdd(room,effect.card,"@up_drawmark"),self.name)
  end
end
--双杀
Fk.skills["double__slash_skill"].onEffect = function(self, room, effect)
  local to = room:getPlayerById(effect.to)
  local from = room:getPlayerById(effect.from)
  local extra_add = fhjdExtraAdd(room,effect.card,"@up_atknum")
  for i=1,2+extra_add do
    if to.dead then return end
    room:damage({
      from = from,
      to = to,
      card = effect.card,
      damage = 1,
      damageType = fk.NormalDamage,
      skillName = self.name
    })
  end
end
--冲杀
Fk.skills["rush__slash_skill"].onEffect = function(self, room, effect)
  local to = room:getPlayerById(effect.to)
  local from = room:getPlayerById(effect.from)
  local extra_add = from.shield
  if extra_add > 0 then
    room:damage({
    from = from,
    to = to,
    card = effect.card,
    damage = extra_add,
    damageType = fk.NormalDamage,
    skillName = self.name
  })
  end
end
--备杀
Fk.skills["drawdis__slash_skill"].onEffect = function(self, room, effect)
  local to = room:getPlayerById(effect.to)
  local from = room:getPlayerById(effect.from)
  local discards = from:getPile("self_discs")
  if #discards > 0 then
    selcard = U.askForArrangeCards(from, "fhjd_selcard", discards, "selcardbydiscs", false, 0, {#discards,1}, {0, 1}, ".", "shelie", {{}, get})[2]
    if selcard and #selcard > 0 then
      room:moveCards({
        from = from.id,
        ids = selcard,
        fromArea = Card.PlayerSpecial,
        to = from.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = from.id,
        skillName = self.name,
      })
    end
  end
  room:damage({
    from = from,
    to = to,
    card = effect.card,
    damage = 1,
    damageType = fk.NormalDamage,
    skillName = self.name
  })
end
--狂杀
Fk.skills["crazy__slash_skill"].onEffect = function(self, room, effect)
  slash_eff_common(self, room, effect,fk.NormalDamage,3)
  local from = room:getPlayerById(effect.from)
  if from:hasSkill("fhjd_yinqing") then
    local selcards = findCards(from,{hurtstatus = 1},room.void)
    handleGetTmpCards(selcards,from,"self_draws",self.name)
  end
end
--独杀
-- Fk.skills["alone__slash_skill"].onEffect = function(self, room, effect)
--   slash_eff_common(self, room, effect,fk.NormalDamage,3)
-- end
-- --独杀
-- Fk.skills["alone__slash_skill"].onEffect = function(self, room, effect)
--   slash_eff_common(self, room, effect,fk.NormalDamage,3)
-- end
------------------各种伤害杀效果函数----------------
Fk:loadTranslationTable{
  ["FHJD_MODE"] = "风华绝代",
  ["fhjd_rule"] = "风华绝代",
  ["#fhjd_rule"] = "风华绝代模式",
  ["fhjd_nolimit"] = "^0^",
  [":fhjd_nolimit"] = "使用牌无距离无次数限制",

  ["#fhjd_sel_upcards"] = "选择卡牌升级",
  ["#fhjd_selcards"] = "选择获得卡牌",
  ["#fhjd_upcard-choose"] = "选择需要升级的卡牌",
  ["#mapselect"] = "前途渺渺，选择岔路口",
  ["fhjd_upcard"] = "可升级",
  ["#cost_cards"] = "选择卡牌消耗",

  -- {"normonster","strongmonster","boss","box","events","shop","rest"}
  ["normonster"] = "普通遭遇",
  [":normonster"] = "小怪物试试手？",
  ["strongmonster"] = "精英遭遇",
  [":strongmonster"] = "有点强。打还是不打？",
  ["boss"] = "BOSS遭遇",
  [":boss"] = "挑战BOSS拿大箱子！",
  ["box"] = "宝箱",
  [":box"] = "箱子里会是什么？",
  ["events"] = "事件",
  [":events"] = "会遇到什么呢？",
  ["shop"] = "商店",
  [":shop"] = "购买卡牌，遗物与删卡",
  ["rest"] = "休息",
  [":rest"] = "回复一定体力值或删卡",

  ["fhjd_draw"] = "抽卡:",
  ["@fhjd_use_num"] = "能量:",
  ["@play_cost"] = "费:",
  ["@hfjd_power"] = "力量",
  ["self_draws"] = "抽卡区",
  ["self_discs"] = "弃卡区",
  ["emy_atk_cards"] = "进攻意图",

  ["@up_damage"] = "伤害+",
  ["@up_strongmark"] = "重伤+",
  ["@up_weakmark"] = "虚弱+",
  ["@up_defmark"] = "护甲+",
  ["@up_shdmgmark"] = "盾杀+",
  ["@up_likeslashmark"] = "杀牌-",
  ["@sub_cost"] = "费-",
  ["@up_drawmark"] = "抽卡+",
  ["@up_atknum"] = "次数+",
  ["@@up_equipall"] = "升级手牌",
  ["@up_power"] = "力量+",

  ["fhjd_yinqing"] = "杀戮",
  [":fhjd_yinqing"] = "每回合开始重置能量（初始为3）。使用牌后耗费卡牌的“费”。当能量不足卡牌的费时则不能打出盖牌。你摸手牌改为从摸牌区摸（初始为5），弃牌逻辑同理",

  ["time limitation: 10s"] = "游戏时长达到10秒",
  ["only you alive"] = "仅剩两人存活",

  [":FHJD_MODE"] = desc_fhjd_mode,
}

return FHJD_MODE
